﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using EntityObjects;

namespace DAL_V1.App_Code.DBBroker
{
    public class AdjustmentDBBroker
    {
        private SSISDataSet ds;
        private SSISDataSetTableAdapters.AdjustmentTableAdapter tda;
        private SSISDataSetTableAdapters.AdjustmentDetailsTableAdapter detailstda;
        private int index;
        private List<Adjustment> ad_list;
        private List<AdjustmentDetails> adDetails_list;
        private AdjustmentDetails adDetails;
        private Adjustment ad;
        private SSISDataSet.AdjustmentDetailsDataTable detailtd;
        private SSISDataSet.AdjustmentDataTable td;

        //singleton
        private static bool instance_flag = false;
        private static AdjustmentDBBroker instance;


        private AdjustmentDBBroker()
        {
            ds = new SSISDataSet();
            tda = new SSISDataSetTableAdapters.AdjustmentTableAdapter();
            detailstda = new SSISDataSetTableAdapters.AdjustmentDetailsTableAdapter();
            //tda.Fill(ds.Adjustment);
            //detailstda.Fill(ds.AdjustmentDetails);
        }

        public static AdjustmentDBBroker getAdjustmentDBBroker()
        {
            if (!instance_flag)
            {
                instance = new AdjustmentDBBroker();
                instance_flag = true;

                return instance;
            }
            else
            {
                return instance;
            }
        }

        public int getAdjMaxSerialNo()
        {
            SSISDataSetTableAdapters.AdjSerialNoTableAdapter tda = new SSISDataSetTableAdapters.AdjSerialNoTableAdapter();
            int j =tda.Fill(ds.AdjSerialNo);
            if (j > 0)
            {
                SSISDataSet.AdjSerialNoRow row = (SSISDataSet.AdjSerialNoRow)ds.AdjSerialNo.Rows[0];
                if (!(row.IsSerialNoNull()))
                {
                    return Convert.ToInt32(row.SerialNo);
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

        public List<Adjustment> selectAllAdjustment()
        {
            tda.Fill(ds.Adjustment);
            ad_list = new List<Adjustment>();
            
            foreach (SSISDataSet.AdjustmentRow row in ds.Adjustment.Rows)
            {
                ad = new Adjustment();
                ad.AdjVoucherId = row.AdjVoucherId;
                ad.AdjDate = Convert.ToString(row.AdjDate);
                ad.AdjStatus = row.AdjStatus;
                ad.ApprovedBy = row.ApprovedBy;
                ad.RequestedBy = row.RequestedBy;
                if (!(row.IsNull("ApprovedDate")))
                {
                    ad.ApprovedDate = Convert.ToString(row.ApprovedDate);
                }
                ad.Remark = row.Remark;
                //Adj Details
                adDetails_list = selectAdjDetailsByAdjVoucherId(row.AdjVoucherId);
                ad.AdjustDetails_list = adDetails_list;

                ad_list.Add(ad);
            }
            return ad_list;
        }


        public int addAdjustment(Adjustment adjustment)
        {
            SSISDataSet.AdjustmentRow newRow = ds.Adjustment.NewAdjustmentRow();
            newRow.AdjVoucherId = adjustment.AdjVoucherId;
            newRow.AdjDate = Convert.ToDateTime(adjustment.AdjDate);
            newRow.AdjStatus = adjustment.AdjStatus;

            newRow.ApprovedBy = adjustment.ApprovedBy;
            newRow.RequestedBy = Convert.ToInt32(adjustment.RequestedBy);
            if (!(newRow.IsApprovedDateNull()))
            {
                newRow.ApprovedDate = Convert.ToDateTime(adjustment.ApprovedDate);
            }
            newRow.Remark = adjustment.Remark;
          
            ds.Adjustment.Rows.Add(newRow);

            return tda.Update(ds.Adjustment);
        }

        private SSISDataSet.AdjustmentRow getRowbyId(String id)
        {
            tda.Fill(ds.Adjustment);
            SSISDataSet.AdjustmentRow row = ds.Adjustment.FindByAdjVoucherId(id);
            return row;
        }

        public int updateAdjustment(Adjustment adjustment, String Id)
        {
            SSISDataSet.AdjustmentRow row = getRowbyId(Id);
            if (row != null)
            {
                if(adjustment.AdjStatus != null && !(adjustment.AdjStatus.Equals("")))
                {
                    row.AdjStatus = adjustment.AdjStatus;
                }
                if(adjustment.ApprovedBy != null && !(adjustment.ApprovedBy.Equals("")))
                {
                    row.ApprovedBy = adjustment.ApprovedBy;
                }
                if (adjustment.Remark != null && !(adjustment.Remark.Equals("")))
                {
                    row.Remark = adjustment.Remark;
                }
            }

            return tda.Update(ds.Adjustment);
        }

        public int deleteAdjustment(String Id)
        {
            detailstda.DeleteByAdjVoucherId(Id);
            return tda.DeleteByAdjVoucherId(Id);
        }

        public Adjustment selectAdjustment(String Id)
        {
            tda.Fill(ds.Adjustment);
            SSISDataSet.AdjustmentRow row = getRowbyId(Id);
            if (row != null)
            {
                ad = new Adjustment();
                ad.AdjVoucherId = row.AdjVoucherId;
                ad.AdjDate = Convert.ToString(row.AdjDate);
                ad.AdjStatus = row.AdjStatus;
                ad.ApprovedBy = row.ApprovedBy;
                ad.RequestedBy = row.RequestedBy;
                if (!(row.IsNull("ApprovedDate")))
                {
                    ad.ApprovedDate = Convert.ToString(row.ApprovedDate);
                }
                ad.Remark = row.Remark;

                //Adj Details
                adDetails_list = selectAdjDetailsByAdjVoucherId(row.AdjVoucherId);
                ad.AdjustDetails_list = adDetails_list;

                return ad;
            }
            else
                return null;
        }

        

        //Adjustment Detail
        public List<AdjustmentDetails> selectAllAdjustmentDetails()
        {
            detailstda.Fill(ds.AdjustmentDetails);
            adDetails_list = new List<AdjustmentDetails>();
          
            foreach (SSISDataSet.AdjustmentDetailsRow r in ds.AdjustmentDetails.Rows)
            {
                adDetails = new AdjustmentDetails();
                adDetails.AdjVoucherId = r.AdjVoucherId;
                adDetails.ItemId = r.ItemId;
                adDetails.Reason = r.Reason;
                adDetails.Quantity = r.Quantity;
                adDetails_list.Add(adDetails);
            }

            return adDetails_list;
        }

        //Select according to Adjustment Id
        public List<AdjustmentDetails> selectAdjDetailsByAdjVoucherId(String id)
        {
            detailstda.Fill(ds.AdjustmentDetails);
            detailtd = detailstda.GetDataByVoucherId(id);

            if (detailtd != null)
            {
                adDetails_list = new List<AdjustmentDetails>();
                for (int i = 0; i < detailtd.Rows.Count; i++)
                {
                    adDetails = new AdjustmentDetails();
                    adDetails.AdjVoucherId = detailtd[i].AdjVoucherId;
                    adDetails.ItemId = detailtd[i].ItemId;
                    adDetails.Reason = detailtd[i].Reason;
                    adDetails.Quantity = detailtd[i].Quantity;
                    adDetails_list.Add(adDetails);
                }
                return adDetails_list;
            }
            else
            {
                return null;
            }
        }

        public int addAdjustmentDetails(AdjustmentDetails adjustmentDetails)
        {
            SSISDataSet.AdjustmentDetailsRow newRow = ds.AdjustmentDetails.NewAdjustmentDetailsRow();
            newRow.AdjVoucherId = adjustmentDetails.AdjVoucherId;
            newRow.ItemId = adjustmentDetails.ItemId;
            newRow.Reason = adjustmentDetails.Reason;
            newRow.Quantity = adjustmentDetails.Quantity;

            ds.AdjustmentDetails.Rows.Add(newRow);
            return detailstda.Update(ds.AdjustmentDetails);
        }

        private SSISDataSet.AdjustmentDetailsRow getRowById(String voucherId, String itemId)
        {
            detailstda.Fill(ds.AdjustmentDetails);
            SSISDataSet.AdjustmentDetailsRow row = ds.AdjustmentDetails.FindByAdjVoucherIdItemId(voucherId, itemId);
            return row;
        }

        public int updateAdjustmentDetails(AdjustmentDetails adjDetails, String voucherId, String itemId)
        {
            SSISDataSet.AdjustmentDetailsRow row = getRowById(voucherId, itemId);
            if (row != null)
            {
                if(adjDetails.Reason != null && !(adjDetails.Reason.Equals("")))
                {
                    row.Reason = adjDetails.Reason;
                }
                if (adjDetails.Quantity != null && adjDetails.Quantity != 0)
                {
                    row.Quantity = adjDetails.Quantity;
                }
            }

            return detailstda.Update(ds.AdjustmentDetails);
        }


        public int deleteAdjustmentDetails(String voucherId, String itemId)
        {
            return detailstda.DeleteByAdjVoucherIdAndItemId(voucherId, itemId);

        }

        public AdjustmentDetails selectAdjustmentDetails(String voucherId, String itemId)
        {
            detailstda.Fill(ds.AdjustmentDetails);
            SSISDataSet.AdjustmentDetailsRow row = getRowById(voucherId, itemId);
            if (row != null)
            {
                adDetails = new AdjustmentDetails();
                adDetails.AdjVoucherId = row.AdjVoucherId;
                adDetails.ItemId = row.ItemId;
                adDetails.Reason = row.Reason;
                adDetails.Quantity = row.Quantity;
                return adDetails;
            }
            else
            {
                return null;
            }
        }
    }
}
